package com.fxf.common.exception;

import com.fxf.common.result.ResponseEnum;
import com.fxf.common.util.RegexValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * @author 饭小范
 * @version 1.0
 * @description: TODO
 * @date 2021/5/27 21:28
 *
 * 自定义断言类来消除if else
 * 可以使用断言来判空来替代if判空操作
 *
 *
 * Assert.notNull(o, "用户不存在.");
 *
 * 用断言的方式封装异常的抛出
 */
@Slf4j
public abstract class Assert {

    /**
     * 断言对象不为空
     * 如果对象obj为空，则抛出异常
     * @param obj
     * @param responseEnum
     */
    public static void notNull(Object obj, ResponseEnum responseEnum){
        if (obj == null){
            log.info("obj is null...............");
            throw new BusinessException(responseEnum);

        }
    }

    /**
     * 断言对象不为空,如果对象obj不为空，则抛出异常
     * @param obj
     * @param responseEnum
     */
    public static void isNull(Object obj, ResponseEnum responseEnum){
        if (obj != null){
            log.info("obj is not null...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言表达式为真，如果不为真，则抛出异常
     * @param expression
     * @param responseEnum
     */
    public static void isTrue(Boolean expression,ResponseEnum responseEnum){
        if (!expression){
            log.info("fail...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象不相等
     * @param obj1
     * @param obj2
     * @param responseEnum
     */
    public static void notEquals(Object obj1,Object obj2,ResponseEnum responseEnum){
        if (obj1.equals(obj2)){
            log.info("equals...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言两个对象相等
     * @param obj1
     * @param obj2
     * @param responseEnum
     */
    public static void equals(Object obj1,Object obj2,ResponseEnum responseEnum){
        if (!obj1.equals(obj2)){
            log.info("not equals...............");
            throw new BusinessException(responseEnum);
        }
    }

    /**
     * 断言参数不为空
     * @param s
     * @param responseEnum
     */
    public static void notEmpty(String s,ResponseEnum responseEnum){
        if (StringUtils.isEmpty(s)){
            log.info("is empty...............");
            throw new BusinessException(responseEnum);
        }
    }


}
